package com.cysion.study.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cysion.study.ktbox.base.BaseAdapter
import com.cysion.study.ktbox.base.BaseViewHolder
import com.cysion.other._setOnClickListener
import com.cysion.study.R
import com.cysion.study.entity.TutorOrderEntity
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.item_tutor_order.view.*

class TutorOrderAdapter(mEntities: MutableList<TutorOrderEntity>, mContext: Context) :
    BaseAdapter<TutorOrderEntity>(mEntities, mContext) {

    companion object {

        //点击事件
        const val preview = 2000
        const val refuse = 2001
        const val agree = 2003
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<TutorOrderEntity> {
        return ProMoneyHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_tutor_order, parent, false)
        )
    }

    class ProMoneyHolder(itemView: View) : BaseViewHolder<TutorOrderEntity>(itemView) {
        override fun fillData(obj: TutorOrderEntity, position: Int) {
            itemView?.apply {
                tv_name.text = obj.fname
                tv_num.text = obj.numbers + "次"
                tv_price.text = "￥" + obj.totalMoney
                tv_orderid.text = obj.orderNo
                tv_address.text = obj.address
                tv_content.text = obj.content
                tv_time.text = obj.payTime
                tv_server_num.text = obj.useNumbers + "次"//已服务次数
                //状态 0.已付款 1.授课同意 2.授课拒绝 3.授课确认中 4.已完成 5.已评价
                var status = obj.status
                if (status == "0") {
                    tv_status.text = "已付款"
                    ll_pj.visibility = View.GONE//家长评价
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.VISIBLE//拒绝
                    tv_agree.visibility = View.VISIBLE//同意
                    et_pj.visibility = View.VISIBLE//拒绝输入框
                } else if (status == "1") {
                    tv_status.text = "待上课"
                    ll_pj.visibility = View.GONE//家长评价
                    tv_ok.visibility = View.VISIBLE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.GONE//拒绝输入框
                } else if (status == "2") {
                    tv_status.text = "授课拒绝"
                    tv_status.setTextColor(resources.getColor(R.color.red_text))
                    ll_pj.visibility = View.GONE//家长评价
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.GONE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.VISIBLE//拒绝输入框
                    et_pj.setText(obj.reason)
                    et_pj.isEnabled = false
                } else if (status == "3") {
                    tv_status.text = "授课确认中"
                    tv_status.setTextColor(resources.getColor(R.color.main_cl))
                    ll_pj.visibility = View.GONE//家长评价
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.GONE//拒绝输入框
                } else if (status == "4") {
                    tv_status.text = "已完成"
                    tv_status.setTextColor(resources.getColor(R.color.main_cl))
                    ll_pj.visibility = View.VISIBLE//家长评价
                    rb_bar.rating = obj.star
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.GONE//拒绝输入框
                } else if (status == "5") {
                    tv_status.text = "已评价"
                    tv_status.setTextColor(resources.getColor(R.color.main_cl))
                    ll_pj.visibility = View.VISIBLE//家长评价
                    rb_bar.rating = obj.star
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.GONE//拒绝输入框
                } else {
                    tv_status.text = "未完成授课"
                    tv_status.setTextColor(resources.getColor(R.color.red_text))
                    ll_pj.visibility = View.GONE//家长评价
                    tv_ok.visibility = View.GONE//我已授课
                    tv_look.visibility = View.VISIBLE//上课记录

                    tv_refuse.visibility = View.GONE//拒绝
                    tv_agree.visibility = View.GONE//同意
                    et_pj.visibility = View.GONE//拒绝输入框
                }


                tv_look._setOnClickListener {
                    mOnTypeClickListener?.invoke(obj, position, preview)
                }
                tv_refuse._setOnClickListener {
                    if (TextUtils.isEmpty(et_pj.text.toString().trim())) {
                        ShowToast("请输入拒绝理由")
                        return@_setOnClickListener
                    }
                    obj.reason = et_pj.text.toString().trim()
                    mOnTypeClickListener?.invoke(obj, position, refuse)
                }
                tv_agree._setOnClickListener {
                    mOnTypeClickListener?.invoke(obj, position, agree)
                }
            }
        }

    }
}